水無瀬の部屋 > Programming > sample > tools > misc > sha256.cpp |
---|
1: //*********************************************************
2: // プロジェクト: TOOLS::SHA256 - Secure Hash Algorithm
3: // ファイル名: sha256.cpp
4: //*********************************************************
5: #include <misc/sha256.h> //
6: #include <header/tooldbg.h> // ASSERT(),
7: #include <header/toolbase.h> //
8: #include <header/snprintf.h> // snprintf(), vsnprintf(),
9: #include <limits.h> // CHAR_BIT,
10:
11:
12: //---------------------------------------------------------
13: // テスト関数 の 宣言
14: //---------------------------------------------------------
15: DECLARE_TESTPROC( test_sha256 );
16:
17:
18: //---------------------------------------------------------
19: // 定数型マクロ の 定義
20: //---------------------------------------------------------
21: #define DBG_ALLOCNAME "SHA256_CreateAlgorithm"
22:
23: //
24: #define SHA256_HASH 8
25: #define SHA256_BLOCK 16
26: #define SHA256_WORK 64
27:
28:
29: //---------------------------------------------------------
30: // 構造体 の 宣言
31: //---------------------------------------------------------
32: typedef struct SHA256_CTX_tag
33: {
34: //
35: DWORD length; // 総書込バイト数
36: DWORD state[ 8 ]; //
37:
38: //
39: DWORD index; // バッファの最終書込バイト位置
40: DWORD buffer[ 16 ]; //
41: } SHA256_CTX;
42:
43:
44: //---------------------------------------------------------
45: // ファイルスコープ関数 の 宣言
46: //---------------------------------------------------------
47: static void SHA256_Transform( DWORD *state, const DWORD *buffer );
48: static inline DWORD ReverseEndian(DWORD x) {return (x << 24) | ((x & 0x0000ff00) << 8) | ((x & 0x00ff0000) >> 8) | (x >> 24);}
49: static bool SHA256_IsValidContext( const SHA256_CTX *context );
50:
51:
52: //*********************************************************
53: // SHA256_CreateAlgorithm
54: //*********************************************************
55: SHA256_CTX *
56: SHA256_CreateAlgorithm
57: (
58: void
59: )
60: {
61: CALLONCE_TESTPROC( test_sha256 ); // [テスト]
62:
63: SHA256_CTX *context = (SHA256_CTX *)malloc( sizeof( *context ) );
64: if ( !context )
65: return null;
66:
67: SHA256_InitData( context );
68: ASSERT( SHA256_IsValidContext( context ) );
69:
70: DBG_LOCK_ALLOCED_MEMORY( context, DBG_ALLOCNAME ); // [DBG]
71: return context;
72: }//SHA256_CreateAlgorithm
73:
74: //*********************************************************
75: // SHA256_DestroyAlgorithm
76: //*********************************************************
77: bool
78: SHA256_DestroyAlgorithm
79: (
80: SHA256_CTX *context
81: )
82: {
83: // パラメタの仮定
84: ASSERT( SHA256_IsValidContext( context ) );
85:
86: DBG_UNLOCK_ALLOCED_MEMORY( context, DBG_ALLOCNAME ); // [DBG]
87: free( context );
88:
89: return true;
90: }//SHA256_DestroyAlgorithm
91:
92: //*********************************************************
93: // SHA256_InitData
94: //*********************************************************
95: bool
96: SHA256_InitData
97: (
98: SHA256_CTX *context
99: )
100: {
101: // パラメタの仮定
102: ASSERT( IsValidPtr( context, sizeof( *context ) ) );
103:
104: context->index = 0;
105: context->length = 0;
106:
107: const DWORD state[] =
108: {
109: 0x6a09e667,
110: 0xbb67ae85,
111: 0x3c6ef372,
112: 0xa54ff53a,
113: 0x510e527f,
114: 0x9b05688c,
115: 0x1f83d9ab,
116: 0x5be0cd19,
117: };
118: memzero( context->buffer, sizeof( context->buffer ) );
119: memcpy( context->state, state, sizeof( context->state ) );
120:
121: ASSERT( SHA256_IsValidContext( context ) );
122: return true;
123: }//SHA256_InitData
124:
125: //*********************************************************
126: // SHA256_AddData
127: //*********************************************************
128: bool
129: SHA256_AddData
130: (
131: SHA256_CTX *context,
132: const void *data,
133: int length
134: )
135: {
136: // パラメタの仮定
137: ASSERT( SHA256_IsValidContext( context ) );
138: ASSERT( 0 < length );
139: ASSERT( IsValidReadPtr( data, length ) );
140:
141: const BYTE *p = (BYTE *)data;
142: while( 0 < length )
143: {
144: const DWORD use = min( ((DWORD)length), (sizeof( context->buffer ) - context->index) );
145: memcpy( ((BYTE *)context->buffer) + context->index, p, use );
146: p += use;
147: context->index += use;
148: context->length += use;
149: length -= use;
150:
151: if ( sizeof( context->buffer ) <= context->index )
152: {
153: SHA256_Transform( context->state, context->buffer );
154: context->index = 0;
155: }
156: }
157:
158: return true;
159: }//SHA256_AddData
160:
161: //*********************************************************
162: // SHA256_GetHash
163: //*********************************************************
164: bool
165: SHA256_GetHash
166: (
167: const SHA256_CTX *context,
168: void *digest,
169: int bufsize
170: )
171: {
172: // パラメタの仮定
173: ASSERT( SHA256_IsValidContext( context ) );
174: ASSERT( 0 < bufsize );
175: ASSERT( IsValidPtr( digest, bufsize ) );
176: DESTROY_BUFFER( digest, bufsize );
177:
178: SHA256_CTX tmp_context;
179: memcpy( &tmp_context, context, sizeof( *context ) );
180: context = null;
181:
182: DWORD dwNumBits[ 2 ];
183: dwNumBits[ 0 ] = ReverseEndian( (tmp_context.length >> 29) );
184: dwNumBits[ 1 ] = ReverseEndian( (tmp_context.length << 3) );
185:
186: BYTE cOne = 0x80;
187: SHA256_AddData( &tmp_context, &cOne, 1 );
188: while( tmp_context.index != sizeof( tmp_context.buffer ) - 8 )
189: {
190: BYTE cZero = 0x00;
191: SHA256_AddData( &tmp_context, &cZero, 1 );
192: }
193:
194: SHA256_AddData( &tmp_context, dwNumBits, 8 );
195: memmove( digest, tmp_context.state, min( bufsize, sizeof( tmp_context.state ) ) );
196:
197: return true;
198: }//SHA256_GetHash
199:
200: //*********************************************************
201: // SHA256_GetHashText
202: //*********************************************************
203: bool
204: SHA256_GetHashText
205: (
206: const SHA256_CTX *context,
207: char *buffer,
208: int bufsize
209: )
210: {
211: // パラメタの仮定
212: ASSERT( SHA256_IsValidContext( context ) );
213: ASSERT( 0 < bufsize );
214: ASSERT( IsValidStringBufferPtr( buffer, bufsize ) );
215: DESTROY_TEXT_BUFFER( buffer, bufsize );
216:
217: //
218: BYTE sha256[ SHA256_HASHSIZE ];
219: VERIFY( SHA256_GetHash( context, sha256, sizeof( sha256 ) ) );
220:
221: //
222: char text[ 1+SHA256_HASHTEXTSIZE ];
223: {
224: char *p = text;
225: {for( int i = 0; i < numof( sha256 ); i += sizeof( DWORD ) )
226: {
227: {for( int j = 0; j < sizeof( DWORD ); ++j )
228: {
229: snprintf( p, numof(text) - (p - text), "%02x", sha256[ i + (sizeof( DWORD ) - j - 1) ] );
230: p = strtail( p );
231: }}
232: }}
233: }
234:
235: //
236: memcpy( buffer, text, min( bufsize, sizeof( text ) ) );
237: buffer[ min( bufsize, numof( text ) ) - 1 ] = '\0';
238:
239: return true;
240: }//SHA256_GetHashText
241:
242:
243: //******************************************************************************************************************
244: //
245: //******************************************************************************************************************
246: //
247: static inline DWORD ROTATE_LEFT(DWORD x, DWORD n) {return (x >> n) | (x << (32 - n));}
248: static inline DWORD S0(DWORD x) {return ROTATE_LEFT(x, 2) ^ ROTATE_LEFT(x, 13) ^ ROTATE_LEFT(x, 22);}
249: static inline DWORD S1(DWORD x) {return ROTATE_LEFT(x, 6) ^ ROTATE_LEFT(x, 11) ^ ROTATE_LEFT(x, 25);}
250: static inline DWORD s0(DWORD x) {return ROTATE_LEFT(x, 7) ^ ROTATE_LEFT(x, 18) ^ (x >> 3);}
251: static inline DWORD s1(DWORD x) {return ROTATE_LEFT(x, 17) ^ ROTATE_LEFT(x, 19) ^ (x >> 10);}
252: static inline DWORD Ch(DWORD x, DWORD y, DWORD z) {return (x & (y ^ z)) ^ z;}
253: static inline DWORD Maj(DWORD x, DWORD y, DWORD z) {return (x & (y | z)) | (y & z);}
254: //
255: const DWORD c_dwK[SHA256_WORK] =
256: {
257: 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
258: 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
259: 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
260: 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
261: 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
262: 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
263: 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
264: 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
265: };
266:
267:
268: //*********************************************************
269: // SHA256_IsValidContext
270: //*********************************************************
271: static
272: bool
273: SHA256_IsValidContext
274: (
275: const SHA256_CTX *context
276: )
277: {
278: VALID_TEST( context );
279: VALID_TEST( IsValidReadPtr( context, sizeof( *context ) ) );
280: VALID_TEST( (0 <= context->index) && (context->index < sizeof( context->buffer ) ) );
281:
282: return true;
283: }//SHA256_IsValidContext
284:
285: //*********************************************************
286: // SHA256_Transform
287: //*********************************************************
288: static
289: void
290: SHA256_Transform
291: (
292: DWORD *state,
293: const DWORD *buffer
294: )
295: {
296: DWORD work[ SHA256_WORK ];
297: {for( int i = 0; i < SHA256_BLOCK; ++i )
298: {
299: work[ i ] = ReverseEndian( buffer[ i ] );
300: }}
301: {for( int i = SHA256_BLOCK; i < SHA256_WORK; ++i )
302: {
303: work[ i ] = s1( work[ i - 2 ] ) + work[ i - 7 ] + s0( work[ i - 15 ] ) + work[ i - 16 ];
304: }}
305:
306: DWORD Hash[ SHA256_WORK + SHA256_HASH ];
307: DWORD *pHash = &Hash[ SHA256_WORK ];
308: memcpy( pHash, state, SHA256_HASH * sizeof( DWORD ) );
309:
310: {for( int i = 0; i < SHA256_WORK; ++i )
311: {
312: pHash--;
313: DWORD dwT1 = pHash[8] + S1(pHash[5]) + Ch(pHash[5], pHash[6], pHash[7]) + c_dwK[i] + work[i];
314: DWORD dwT2 = S0(pHash[1]) + Maj(pHash[1], pHash[2], pHash[3]);
315: pHash[0] = dwT1 + dwT2;
316: pHash[4] += dwT1;
317: }}
318:
319: {for( int i = 0; i < SHA256_HASH; ++i )
320: {
321: state[ i ] += pHash[ i ];
322: }}
323: }//SHA256_Transform
324:
325:
326: //******************************************************************************************************************
327: // TEST
328: //******************************************************************************************************************
329:
330:
331: #ifdef _DEBUG // デバッグ時のみ
332:
333:
334: //*********************************************************
335: // test_sha256
336: //*********************************************************
337: DEFINE_TESTPROC( test_sha256 )
338: {
339: //---------------------------------------------------------
340: // 定数 の テスト
341: //---------------------------------------------------------
342:
343: //---------------------------------------------------------
344: // ファイルスコープ関数 の テスト
345: //---------------------------------------------------------
346:
347: //---------------------------------------------------------
348: // 公開関数 の テスト
349: //---------------------------------------------------------
350:
351: static const BYTE testcase[] =
352: {
353: 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
354: 0x2e, 0xd1, 0x73, 0x47, 0x93, 0xbb, 0x71, 0x23, 0x54, 0x0f, 0x9a, 0x5b, 0xc3, 0xa1, 0xb4, 0x39, 0x14, 0x24, 0x3f, 0xad, 0xf8, 0x80, 0x69, 0xb8, 0xfa, 0x1c, 0x8d, 0x41, 0xef, 0xdf, 0xfa, 0xbd,
355: 0x41, 0xc1, 0x5c, 0x29, 0x2d, 0xe5, 0x4f, 0xf7, 0x07, 0x71, 0x9e, 0xd9, 0xb1, 0x79, 0x35, 0x0a, 0xc4, 0xe1, 0xb0, 0xfa, 0xa1, 0x7c, 0x0f, 0xdd, 0xaf, 0x58, 0x75, 0x7b, 0x22, 0x21, 0xfa, 0xcc,
356: 0x67, 0xbc, 0x6d, 0x51, 0x65, 0x08, 0xbb, 0x60, 0x8a, 0x97, 0xf4, 0x1a, 0x5e, 0xcd, 0x5a, 0x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03, 0xf8, 0x36, 0x95, 0xcb, 0x38, 0xb1, 0x3e, 0x40, 0x0f,
385: };
386:
387: // SHA256
388: SHA256_CTX *context = SHA256_CreateAlgorithm();
389:
390: // 一気にハッシュ
391: {for( int i = 1; (i * SHA256_HASHSIZE) < numof( testcase ); ++i )
392: {
393: SHA256_InitData( context );
394: SHA256_AddData( context, testcase, i * SHA256_HASHSIZE );
395:
396: BYTE sha[ SHA256_HASHSIZE ];
397: {for( int j = 1; j < numof( sha ); ++j )
398: {
399: SHA256_GetHash( context, sha, j );
400: VERIFY( 0 == memcmp( sha, testcase + (i * SHA256_HASHSIZE), j ) );
401: }}
402: }}
403:
404: // 徐々にハッシュ
405: SHA256_InitData( context );
406: {for( int i = 1; (i * SHA256_HASHSIZE) < numof( testcase ); ++i )
407: {
408: {for( int j = 0; j < SHA256_HASHSIZE; ++j )
409: {
410: SHA256_AddData( context, &testcase[ j + ((i-1) * SHA256_HASHSIZE) ], 1 );
411: }}
412:
413: BYTE sha[ SHA256_HASHSIZE ];
414: SHA256_GetHash( context, sha, numof( sha ) );
415: VERIFY( 0 == memcmp( sha, testcase + (i * SHA256_HASHSIZE), numof( sha ) ) );
416: }}
417:
418: VERIFY( SHA256_DestroyAlgorithm( context ) );
419:
420: }//test_sha256
421:
422:
423: #endif // #ifdef _DEBUG
424:
425:
426: //** end **
参照:
水無瀬の部屋 > sample > tools > misc > sha256.cpp |
---|
このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/tools/misc/sha256_cpp.shtml